﻿using System;
using System.Data.Entity;
using System.Linq;
using System.Windows.Forms;
using AppAcc.Models;

namespace AppAcc.Views.DanhMuc
{
    public partial class FormHangHoaCapNhap : DevComponents.DotNetBar.RibbonForm
    {
        public FormHangHoaCapNhap()
        {
            InitializeComponent();
        }
        //private AppAccContext db = new AppAccContext();
        private PhanMenKeToanEntities1 db = new PhanMenKeToanEntities1();
        public int id = 0;
        private void txtTenHangHoa_TextChanged(object sender, EventArgs e)
        {
            KiemTra();
        }

        private void cbbLoaiHangHoa_SelectedIndexChanged(object sender, EventArgs e)
        {
            KiemTra();
        }

        private void txtDienGiai_TextChanged(object sender, EventArgs e)
        {
            KiemTra();
        }
        private void cbbDonViTinh_SelectedIndexChanged(object sender, EventArgs e)
        {
            KiemTra();
        }
        private void bttLuu_Click(object sender, EventArgs e)
        {
            Edit();
        }

        private void bttThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        void GetListLoaiHang()
        {
            cbbLoaiHangHoa.DataSource = db.LoaiHangs.ToList();
            cbbLoaiHangHoa.DisplayMember = "TenLoaiHang";
            cbbLoaiHangHoa.ValueMember = "Id";
        }

        void GetListDonViTinh()
        {
            cbbDonViTinh.DataSource = db.DonViTinhs.ToList();
            cbbDonViTinh.DisplayMember = "TenDonViTinh";
            cbbDonViTinh.ValueMember = "Id";
        }
        void KiemTra()
        {
            if (txtDienGiai.Text != "" && Convert.ToInt32(cbbDonViTinh.SelectedValue) > 0 && txtTenHangHoa.Text != "" &&
                Convert.ToInt32(cbbLoaiHangHoa.SelectedValue) != 0)
            {
                bttLuu.Enabled = true;
            }
            else
            {
                bttLuu.Enabled = false;
            }
        }

        void Edit()
        {
            try
            {
                var t = db.HangHoas.Find(id);
                t.DonViTinhId = Convert.ToInt32(cbbDonViTinh.SelectedValue);
                t.DienGiai = txtDienGiai.Text;
                t.LoaiHangId = Convert.ToInt32(cbbLoaiHangHoa.SelectedValue);
                t.SoLuong = t.SoLuong;
                t.TenHangHoa = txtTenHangHoa.Text;
                db.Entry(t).State = EntityState.Modified;
                db.SaveChanges();
                MessageBox.Show("Cập nhập thành công!");
                this.Close();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }

        private void FormHangHoaCapNhap_Load(object sender, EventArgs e)
        {
            
            if (id != 0)
            {
                GetListLoaiHang();
                GetListDonViTinh();
                var t = db.HangHoas.Find(id);
                txtDienGiai.Text = t.DienGiai;
                cbbDonViTinh.SelectedValue = t.DonViTinhId;
                txtTenHangHoa.Text = t.TenHangHoa;
                cbbLoaiHangHoa.SelectedValue = t.LoaiHangId;
                KiemTra();
            }
            else
            {
                MessageBox.Show("Không tải được dữ liệu!");
                this.Close();
            }
        }


    }
}
